<?php

namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Validator, Redirect, Session, Captcha;
use App\Http\Model\User\User;
use App\Http\Model\Article\Article;
use App\Http\Model\Article\Tags;
class  UserController extends Controller{
    public  function  welcome(Request $request){
        $article_list = Article::orderBy('updated_at','asc')->paginate(50);
        $tags_list = Tags::orderBy('tags_article_num','asc')->paginate(50);
        $hot_hits_article_list = Article::orderBy('article_hits','asc')->paginate();
        if(session('user')){

        }
        return view('welcome', ['article_list' => $article_list,'hot_hits_article_list'=>$hot_hits_article_list,'tags_list'=>$tags_list]);
    }
    public  function  login_index(Request $request){
        return view('user.user.login');
    }
    public  function  login_submit(Request $request){
        $input=Input::all();
        $messages = [
            'user_email.required'=>'邮箱必填',
            'user_pwd.required'=>'密码必填',
        ];
        $validator = Validator::make($input, [
            'user_pwd' => 'required',
            'user_email' => 'required',
        ],$messages);

        if ($validator->fails()) {
            return redirect('user/login/index')
                ->withErrors($validator)
                ->withInput();
        }
        $user=User::where('user_email',$input['user_email'])
            ->first();
        if($user){
            $user_info=[
                'user_name' =>$user->user_name,
                'user_email' =>$input['user_email'],
                'user_id'=>$user->user_id
            ];
            Session::put('user_info', $user_info);
            return redirect('/index');
           // return view('welcome')->with('user_info',$user_info);

        }else{
            return redirect('user/login/index')
                ->withErrors('邮箱或密码有误')
                ->withInput();
        }



    }
    public  function  register(Request $request){
        //return view('admin.index');
        return view('user.user.reg');
    }
    public  function  register_submit(Request $request){
        $input=Input::all();
        $messages = [
            'user_name.required'=>'用户名必填',
            'user_pwd.required'=>'密码必填',
            'user_email.required'=>'邮箱必填',
        ];
        $validator = Validator::make($input, [
            'user_name' => 'required|max:255',
            'user_pwd' => 'required',
            'user_email' => 'required',
        ],$messages);

        if ($validator->fails()) {
            return redirect('user/reg')
                ->withErrors($validator)
                ->withInput();
        }

        $User=User::insert([
            [
                'user_name' =>$input['user_name'],
                'user_pwd' =>$input['user_pwd'],
                'user_email' =>$input['user_email'],
                'create_time' =>date('Y-m-d H:i:s'),
            ]
        ]);
        $user_id=User::where('user_email',$input['user_email'])
            ->first();

        if($User){
            $user_info=[
                'user_name' =>$input['user_name'],
                'user_email' =>$input['user_email'],
                'user_id'=>$user_id
            ];
            Session::put('user_info', $user_info);
            return redirect('/');

        }else{
            return json_success('1','注册失败');
        }
        return 2;
    }
    public  function  home(Request $request){
        //return view('admin.index');
        return view('user.user.home');
    }
    public  function  home_set(Request $request){
        //return view('admin.index');
        return view('user.user.set');
    }
    public  function  login_out(Request $request){
        //return view('admin.index');
        Session::forget('user_info');
        return view('welcome');
    }
}